﻿using App.Common;
using QxFramework.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class MainUI : UIBase
{
    private int PlayerID = 0;

    public int CurrentID
    {
        get
        {
            return _currentID;
        }
        set
        {
            Alph = 3;
            if ((_currentID < value && (value - _currentID == 1)) ||
                (_currentID == (GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList.Count - 1) && value == 0))
            {
                TargetNum = -200;
            }
            else
            {
                TargetNum = 0;
            }
            _currentID = value;
        }
    }
    private int _currentID;

    public override void OnDisplay(object args)
    {
        base.OnDisplay(args);
        PlayerID = int.Parse(args.ToString());
        Alph = 0;
        CollectObject();
        UpdateItems();
        UpdateSkillID();
        canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
        _gos["Text"].GetComponent<Text>().text = PlayerID.ToString() + "P";
    }
    private void Update()
    {
        UpdateHp();
        UpdateLp();
        UpdateMp();
        UpdateSkillColors();
        UpdataSkills();
    }

    Canvas canvas;
    void UpdateUIPosition()
    {
        if (!GameMgr.Get<IMonoManager>().GetAllMono()._MonoList.ContainsValue(GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono))
        {
            UIManager.Instance.Close(this);
            return;
        }
        if (GameMgr.Get<IMonoManager>().GetObj(GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono) == null)
        {
            return;
        }
        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform,
            Camera.main.WorldToScreenPoint(GameMgr.Get<IMonoManager>().GetObj(GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono).transform.position), 
            canvas.worldCamera, out Vector2 pos);
        _gos["Pivot"].GetComponent<RectTransform>().localPosition = new Vector3(pos.x, pos.y,0);
    }
    private void LateUpdate()
    {
        UpdateUIPosition();
    }

    float FalseHp;
    float FalseMp;
    float FalseLp;
    void UpdateHp()
    {
        HP hpData = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.HPData;
        _gos["HpBar"].GetComponent<Slider>().value = ((float)hpData.HPValue) / ((float)hpData.HPCeiling);
        if (FalseHp < hpData.HPValue)
        {
            FalseHp = hpData.HPValue;
        }
        else
        {
            FalseHp -= 0.2f;
        }
        _gos["HpFalseBar"].GetComponent<Slider>().value = ((float)FalseHp) / ((float)hpData.HPCeiling);
    }
    void UpdateLp()
    {
        LP lpData = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.LPData;
        _gos["LpBar"].GetComponent<Slider>().value = ((float)lpData.LPValue) / ((float)lpData.LPCeiling);
        if (FalseLp < lpData.LPValue)
        {
            FalseLp = lpData.LPValue;
        }
        else
        {
            FalseLp -= 20f;
        }
        _gos["LpFalseBar"].GetComponent<Slider>().value = ((float)FalseLp) / ((float)lpData.LPCeiling);
    }
    void UpdateMp()
    {
        MP mpData = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.MPData;
        SkillBase skillBase = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList[currentID];
        _gos["MpBar"].GetComponent<Slider>().value = ((float)mpData.MPValue) / ((float)mpData.MPCeiling);
        if (FalseMp < mpData.MPValue)
        {
            FalseMp = mpData.MPValue;
        }
        else
        {
            FalseMp -= 20f;
        }
        _gos["MpFalseBar"].GetComponent<Slider>().value = ((float)FalseMp) / ((float)mpData.MPCeiling);

        Mono monoBase = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono;
        Image Img = _gos["CurSkill"].transform.Find("Image").GetComponent<Image>();
        Img.sprite = ResourceManager.Instance.Load<Sprite>("Texture/UI/Icon/" + skillBase.IconName);

        _gos["SkillText"].GetComponentInChildren<Text>().text = skillBase.Name;

        //Img.color =
        //    (monoBase.SkillList[currentID].skillMPData.MPValue < monoBase.SkillList[currentID].skillMPData.MPUse + Data.Instance.TableAgent.GetInt("AllBuff", GetAttrID(monoBase).ToString(), "MPPlus"))?
        //    new Color(Img.color.r, Img.color.g, Img.color.b, 0.5f) : new Color(Img.color.r, Img.color.g, Img.color.b, 1f);
    }

    float Alph;
    void UpdateSkillColors()
    {
        if (Alph < 0)
        {
            Alph = 0;
        }
        else
        {
            Alph -= 0.05f;
        }
        foreach(Transform t in _gos["Skill"].transform)
        {
            t.GetComponent<Image>().color = new Color(
                t.GetComponent<Image>().color.r, t.GetComponent<Image>().color.g, t.GetComponent<Image>().color.b
                , Alph);
        }
        _gos["SkillText"].GetComponentInChildren<Text>().color = new Color(1, 1, 1, _gos["SkillBoard"].GetComponent<Image>().color.a);
    }
    void UpdataSkills()
    {
        if (CurrentID != GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].CurrentSkillID)
        {
            CurrentID = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].CurrentSkillID;
        }
        AdjustSkillBarTempNum();
        UpdateSkillIcon();
    }
    int SkillBarTempNum;
    int TargetNum;
    bool IsChanging;
    void AdjustSkillBarTempNum()
    {
        if(SkillBarTempNum< TargetNum)
        {
            IsChanging = true;
            SkillBarTempNum += 20;
            _gos["SkillBoard"].GetComponent<HorizontalLayoutGroup>().padding.left = SkillBarTempNum;
            _gos["SkillBoard"].GetComponent<HorizontalLayoutGroup>().enabled = false;
            _gos["SkillBoard"].GetComponent<HorizontalLayoutGroup>().enabled = true;
        }
        else if (SkillBarTempNum > TargetNum)
        {
            IsChanging = true;
            _gos["SkillBoard"].GetComponent<HorizontalLayoutGroup>().padding.left = SkillBarTempNum;
            _gos["SkillBoard"].GetComponent<HorizontalLayoutGroup>().enabled = false;
            _gos["SkillBoard"].GetComponent<HorizontalLayoutGroup>().enabled = true;
            SkillBarTempNum -= 20;
        }
        else if(IsChanging)
        {
            IsChanging = false;
            UpdateSkillID();
        }
    }
    void UpdateSkillID()
    {
        TargetNum = -100;
        SkillBarTempNum = -100;
        GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].realTimeStatus.IsChangingSkill = false;
        _gos["SkillBoard"].GetComponent<HorizontalLayoutGroup>().padding.left = SkillBarTempNum;
        _gos["SkillBoard"].GetComponent<HorizontalLayoutGroup>().enabled = false;
        _gos["SkillBoard"].GetComponent<HorizontalLayoutGroup>().enabled = true;

        currentID = CurrentID;

        PriviousID = currentID - 1;
        if (PriviousID < 0)
        {
            PriviousID = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList.Count - 1;
        }
        PriviousID2 = PriviousID - 1;
        if (PriviousID2 < 0)
        {
            PriviousID2 = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList.Count - 1;
        }
        NextID = currentID + 1;
        if (NextID >= GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList.Count)
        {
            NextID = 0;
        }
        NextID2 = NextID + 1;
        if (NextID2 >= GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList.Count)
        {
            NextID2 = 0;
        }
    }
    int PriviousID;
    int PriviousID2;
    int currentID;
    int NextID;
    int NextID2;
    void UpdateSkillIcon()
    {

        MP mpData = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.MPData;

        SkillBase skillBase1 = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList[PriviousID2];
        SkillMPData skillMPData1 = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList[PriviousID2].skillMPData;
        _gos["SkillIconPrevious2"].transform.Find("SkillIcon").GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/UI/Icon/" + skillBase1.IconName);
        _gos["SkillIconPrevious2"].GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/UI/Icon/" + skillBase1.IconName);
        //_gos["SkillIconPrevious2"].transform.Find("SkillIcon").GetComponent<Image>().fillAmount = 1 - ((float)skillMPData1.MPValue) / ((float)mpData.MPCeiling);
        SkillBase skillBase0 = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList[PriviousID];
        SkillMPData skillMPData0 = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList[PriviousID].skillMPData;
        _gos["SkillIconPrevious"].transform.Find("SkillIcon").GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/UI/Icon/" + skillBase0.IconName);
        _gos["SkillIconPrevious"].GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/UI/Icon/" + skillBase0.IconName);
        //_gos["SkillIconPrevious"].transform.Find("SkillIcon").GetComponent<Image>().fillAmount = 1 - ((float)skillMPData0.MPValue) / ((float)mpData.MPCeiling);
        SkillBase skillBase2 = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList[currentID];
        SkillMPData skillMPData2 = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList[currentID].skillMPData;
        _gos["SkillIconCurrent"].transform.Find("SkillIcon").GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/UI/Icon/" + skillBase2.IconName);
        _gos["SkillIconCurrent"].GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/UI/Icon/" + skillBase2.IconName);
        //_gos["SkillIconCurrent"].transform.Find("SkillIcon").GetComponent<Image>().fillAmount = 1 - ((float)skillMPData2.MPValue) / ((float)mpData.MPCeiling);
        SkillBase skillBase3 = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList[NextID];
        SkillMPData skillMPData3 = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList[NextID].skillMPData;
        _gos["SkillIconNext"].transform.Find("SkillIcon").GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/UI/Icon/" + skillBase3.IconName);
        _gos["SkillIconNext"].GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/UI/Icon/" + skillBase3.IconName);
        //_gos["SkillIconNext"].transform.Find("SkillIcon").GetComponent<Image>().fillAmount = 1 - ((float)skillMPData3.MPValue) / ((float)mpData.MPCeiling);
        SkillBase skillBase4 = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList[NextID2];
        SkillMPData skillMPData4 = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.SkillList[NextID2].skillMPData;
        _gos["SkillIconNext2"].transform.Find("SkillIcon").GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/UI/Icon/" + skillBase4.IconName);
        _gos["SkillIconNext2"].GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/UI/Icon/" + skillBase4.IconName);
        //_gos["SkillIconNext2"].transform.Find("SkillIcon").GetComponent<Image>().fillAmount = 1 - ((float)skillMPData4.MPValue) / ((float)mpData.MPCeiling);

    }

    public void UpdateItems()
    {
        string pivot = (PlayerID == 0) ? "Pivot2" : "Pivot1";
        for (int i = 0; i < _gos[pivot].transform.childCount; i++)
        {
            Destroy(_gos[pivot].transform.GetChild(i).gameObject);
        }
        for (int i = 0; i < GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.ItemList.Count; i++)
        {
            int ID = 0;
            ID = i;
            GameObject clone = ResourceManager.Instance.Instantiate("Prefabs/UI/ItemsItm", _gos[pivot].transform);
            clone.transform.SetParent(_gos[pivot].transform);
            string Text = GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.ItemList[i].ToString();
            clone.GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/Items/"+ Data.Instance.TableAgent.GetString("AllItem", Text, "Sprite"));
            clone.GetComponent<Button>().onClick.AddListener(()=> {
                UIManager.Instance.Open("DialogWindowUI", 3, args: new DialogWindowUI.DialogWindowUIArg("道具功能", Data.Instance.TableAgent.GetString("AllItem", Text, "Description"), null, "确定", () => {
                }));
            });
        }
    }


    private int GetAttrID(Mono mono)
    {
        switch (mono.Attr)
        {
            case MonoAttribute.None:
                return 0;
            case MonoAttribute.Fire:
                return 1;
            case MonoAttribute.Ice:
                return 2;
            case MonoAttribute.Shine:
                return 3;
            case MonoAttribute.Dark:
                return 4;
            case MonoAttribute.Steam:
                return 5;
            case MonoAttribute.Electric:
                return 6;
            case MonoAttribute.Oil:
                return 7;
            default:
                return 0;
        }
    }

}
